<?php 
include('../../Connections/KRAPPERCON.php');
$oper  = '';
if (isset($_POST['oper'])){
$oper = $_POST['oper'];
}

$desc = '';
$costo = '';
$prec = '';
$reem = '';

$cod = $_POST['t_cm_codart'];
$alm = $_POST['cbo_cm_alm'];
$almc_cod[] = -1;
$almc_nom[] = ' ';
$sql = '';

if ($oper == 'view'){

if (trim($cod) == ''){
echo "<b>Debe ingresar un Codigo a Buscar</b><br>";
}

$opc = getVal('opcion');
//echo $opc;
if ($opc == 1){
$sql= "call sp_getArticulo('".$cod."');";
}
if ($opc == 2){
$sql= "call sp_getArticuloAll('".$cod."');";
}

$found = false;

$res = $mysqli->query($sql);
if ($res){
	if ($row = $res->fetch_assoc()){
		$found = true;
		$desc = $row['ArticuloName'];
		$costo = $row['ArticuloCos'];
		$prec = $row['ArticuloPre'];
		$reem = $row['ArticuloIDNew'];
	}
$res->close();
$mysqli->check_result();
}



$sqla = 'call sp_getAlmacenes(0,1)';
$res = $mysqli->query($sqla);
if ($res){
	while($row = $res->fetch_assoc()){
		$almc_cod[] = $row['AlmacenID'];
		$almc_nom[] = $row['AlmacenNom'];
	}
	$res->close();
	$mysqli->check_result();
}

if (!$found){
	echo "<b>No registrado en Lista Codigos Toyota</b><br>";
}
else {
?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">

  <tr>
    <td>Descripcion:</td>
    <td><?php echo $desc; ?></td>
    <td>Reemplazo :</td>
    <td><?php echo $reem; ?></td>
  </tr>
  <tr>
    <td class="noView">Costo:</td>
    <td class="noView"><?php echo $costo; ?></td>
    <td>Precio:</td>
    <td><?php echo $prec; ?></td>
  </tr>
</table>
<?php
} ?>
<br />
<table width="100%" border="1" class="tblSort" rules="all" cellspacing="0" cellpadding="0">
<thead>
  <tr>
    <th>Almacen</th>
    <th>Ubicacion</th>
    <th>Stk.Libre</th>
    <th>Stk.Total</th>
  </tr>
  </thead>
  <?php
  $sql1 = "call sp_getAlmacenesArt('".$cod."','".$alm."')";
	$res1 = $mysqli->query($sql1);
	$t1 = 0;
	$t2 = 0;

if ($res1){	
	while($row = $res1->fetch_assoc()){
	$na = $almc_nom[array_search($row['AlmacenID'],$almc_cod)];
	$t1 += $row['StockLib'];
	$t2 += $row['StockTot'];
  ?>
  <tbody>
  <tr>
    <td><?php echo $row['AlmacenID'].' '.$na; ?></td>
    <td><?php echo $row['ArticuloUbi']; ?></td>
    <td class="qt"><?php echo $row['StockLib']; ?></td>
    <td class="qt"><?php echo $row['StockTot']; ?></td>
  </tr>
  </tbody>
  <?php 
  }
  $res1->close();
  $mysqli->check_result();
  }
  ?>
  <tfoot>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <th class="qt"><?php echo $t1; ?></th>
    <th class="qt"><?php echo $t2; ?></th>
  </tr>
  </tfoot>
</table>
&nbsp;
<table width="100%" border="1" rules="all" class="tblSort" cellspacing="0" cellpadding="0">
<thead>
  <tr>
    <th>Proveedor</th>
    <th>Doc.Compras</th>
    <th>Creacion</th>
    <th>Entrega</th>
    <th>Almacen</th>
    <th>Ctd.Pedida</th>
    <th>Ctd.Pendiente</th>
  </tr>
  </thead>
    <tbody>
  <?php 
  
$sql3 = "call sp_getComprasArt('".$cod."','".$alm."')";
$res3 = $mysqli->query($sql3);
	$tt1 = 0;
	$tt2 = 0;
  
  if ($res3){
  	while($row = $res3->fetch_assoc()){
	$na = $almc_nom[array_search($row['AlmacenID'],$almc_cod)];
	$tt1 += $row['DetOrdQtp'];
	$tt2 += ($row['DetOrdQtp']-$row['DetOrdQta']);
  ?>
  <tr>
    <td><?php echo ($row['ProveedNom']); ?></td>
    <td><?php echo ($row['OrdenNro']); ?></td>
    <td><?php echo ($row['OrdenFecCre']); ?></td>
    <td><?php echo ($row['OrdenFecEnt']); ?></td>
    <td><?php echo ($na); ?></td>
    <td class="qt"><?php echo ($row['DetOrdQtp']); ?></td>
    <td class="qt"><?php echo ($row['DetOrdQtp']-$row['DetOrdQta']); ?></td>
  </tr>
  <?php 
 	 }
	 ?>
  <?php 
  $res3->close();
  $mysqli->check_result();
  }
  ?>
  </tbody>
  <tfoot>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <th class="qt"><?php echo $tt1; ?></th>
    <th class="qt"><?php echo $tt2; ?></th>
  </tr>
  </tfoot>
</table>
<?php
 }
  ?>
